<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>检测数据改变的原理</title>
</head>
<body>
    <script type="text/javascript">
        let data = {
            name:'zw',
            addr:'bj'
        }

        // 创建监视实例对象
        const obs = new Observer(data)

        console.log(obs)
        // 准备vm对象
        let vm = {}
        vm._data = data = obs

        // 构造方法
        function Observer(obj){
            const keys = Object.keys(obj)
            keys.forEach((k) =>{
                Object.defineProperty(this,k,{
                    get(){
                        return obj[k]
                    },
                    set(val){
                        console.log(`${k}被修改了`)
                        obj[k] = val
                    }
                })
            })
        }

   </script>
    
</body>
</html>